package com.jinjia.ordersys.service.impl;

import com.jinjia.ordersys.dao.GoodsMapper;
import com.jinjia.ordersys.dao.OrderMapper;
import com.jinjia.ordersys.domain.Goods;
import com.jinjia.ordersys.domain.Order;
import com.jinjia.ordersys.dto.OrderDto;
import com.jinjia.ordersys.dto.QueryOrdersDTO;
import com.jinjia.ordersys.service.OrderService;
import com.jinjia.ordersys.util.CommonUtil;
import com.jinjia.ordersys.util.ConstantUtil;
import com.jinjia.ordersys.vo.AddGoogsVo;
import com.jinjia.ordersys.vo.AddOrderVo;
import com.jinjia.ordersys.vo.CustomerInfoVO;
import com.jinjia.ordersys.vo.QueryHistoryOrderVo;
import com.jinjia.ordersys.vo.QueryOrderVo;
import com.jinjia.ordersys.yilianyun.Methods;

import java.util.ArrayList;
import java.util.Collections;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 订单service-实现类
 * @Param:
 * @return:
 * @Author: fenghouzhi
 * @Date: 2018/10/11
 * @Time: 4:56 PM
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public List<OrderDto> getAllOrderDto(QueryOrderVo queryOrderVo) {
        List<OrderDto> orderDtoList = orderMapper.getAllOrderDto(queryOrderVo);
        if (orderDtoList != null && orderDtoList.size() > 0) {
            for (OrderDto orderDto : orderDtoList) {
                List<Goods> goodsList = goodsMapper.getGoodsListByOrderId(orderDto.getId());
                orderDto.setGoodsList(goodsList);
            }
            return orderDtoList;
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    public List<OrderDto> getDeleleOrderDtos(QueryOrdersDTO queryOrdersDTO) {
        List<OrderDto> orderDtoList = orderMapper.getDeleleOrderDtos(queryOrdersDTO);
        if (orderDtoList != null && orderDtoList.size() > 0) {
            for (OrderDto orderDto : orderDtoList) {
                List<Goods> goodsList = goodsMapper.getGoodsListByOrderId(orderDto.getId());
                orderDto.setGoodsList(goodsList);
            }
            return orderDtoList;
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    public List<OrderDto> getStopOrderDtos(QueryOrdersDTO queryOrdersDTO) {
        List<OrderDto> orderDtoList = orderMapper.getStopOrders(queryOrdersDTO);
        if (orderDtoList != null && orderDtoList.size() > 0) {
            for (OrderDto orderDto : orderDtoList) {
                List<Goods> goodsList = goodsMapper.getGoodsListByOrderId(orderDto.getId());
                orderDto.setGoodsList(goodsList);
            }
            return orderDtoList;
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    public List<OrderDto> getAllHistoryOrdersDto(QueryHistoryOrderVo queryHistoryOrderVo) {
        List<OrderDto> orderDtoList = orderMapper.getAllHistoryOrdersDto(queryHistoryOrderVo);
        if (orderDtoList != null && orderDtoList.size() > 0) {
            for (OrderDto orderDto : orderDtoList) {
                List<Goods> goodsList = goodsMapper.getGoodsListByOrderId(orderDto.getId());
                orderDto.setGoodsList(goodsList);
            }
            return orderDtoList;
        } else {
            return new ArrayList<>(0);
        }
    }

    @Override
    public OrderDto getOrderDetail(Long orderId) {

        try {
            OrderDto orderDto = orderMapper.getOrderDetail(orderId);
            List<Goods> goodsList = goodsMapper.getGoodsListByOrderId(orderDto.getId());
            orderDto.setGoodsList(goodsList);
            log.info("order dto info-------订单id为 {} 下有商品 {} 个", orderDto.getId(), goodsList.size());
            return orderDto;
        } catch (NullPointerException e) {
            log.info(e.getMessage(), e);
            return null;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addOrder(AddOrderVo addOrderVo) {
        try {
            Order order = new Order();
            BeanUtils.copyProperties(addOrderVo, order);
            order.setOrderNo(CommonUtil.getUuid());
            order.setOrderStatus("0");
            orderMapper.addOrder(order);
            log.info("order info:   " + order);
            for (AddGoogsVo addGoogsVo : addOrderVo.getAddGoodsVoList()) {
                addGoogsVo.setOrderId(order.getId());
            }
            goodsMapper.addGoodsForOrder(addOrderVo.getAddGoodsVoList());

            return 1;
        } catch (Exception e) {
            log.info(e.getMessage(), e);
            return 0;
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Integer editOrder(AddOrderVo addOrderVo) {
        Order order = new Order();
        BeanUtils.copyProperties(addOrderVo, order);
        int editOrderResult = orderMapper.editOrder(order);
        if (editOrderResult < 1) {
            throw new RuntimeException("编辑更新订单信息异常");
        }
        if (null != addOrderVo.getAddGoodsVoList() && addOrderVo.getAddGoodsVoList().size() > 0) {
            for (AddGoogsVo googsVo : addOrderVo.getAddGoodsVoList()) {
                int editGoodForOrderResult = goodsMapper.updateGoodsForOrder(googsVo);
                if (editGoodForOrderResult < 1) {
                    throw new RuntimeException("编辑更新订单下商品信息异常");
                }
            }
        }
        return editOrderResult;
    }

    @Override
    public Integer updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }

    @Override
    public Integer deleteOrder(Long orderId) {
        return orderMapper.deleteOrderById(orderId);
    }

    @Override
    public List<CustomerInfoVO> getCustomersByPersonId(Long personId) {
        return orderMapper.getCustomersByPersonId(personId);
    }

    @Override
    public String printOrder(Long orderId) {
        String printResultMsg;
        OrderDto orderDto = getOrderDetail(orderId);
        if (null == orderDto) {
            throw new RuntimeException("为找到该订单");
        }
        String content = generatePrintContentMethod(orderDto);
        try {
            printResultMsg = Methods.getInstance()
                    .print(ConstantUtil.PRINT_MACHINE_CODE, content, orderDto.getOrderNo());
            return printResultMsg;
        } catch (Exception e) {
            log.info("打印服务异常");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成打印内容
     */
    private String generatePrintContentMethod(OrderDto orderDto) {
        String content;
        StringBuilder goodsContent = new StringBuilder();
        for (Goods goods : orderDto.getGoodsList()) {
            goodsContent.append(goods.getGoodsName() + "  " + goods.getGoodsQuantity() + "\n");
        }
        content = "<FH><FB><center>订单凭据</center></FB></FH>\n"
                + "********************************\n"
                + "<FH>\n"
                + "配送方式：商家/物流配送\n"
                + "配送姓名：" + orderDto.getCustomerName() + "\n"
                + "电话:" + orderDto.getCustomerPhone() + "\n"
                + "地址：" + orderDto.getCustomerAddress() + "\n"
                + "日期：" + orderDto.getOrderTime() + "\n"
                + "单号：" + orderDto.getOrderNo() + "</FH>\n"
                + "********************************\n<FH>\n"
                + "名称 数量(吨)\n"
                + goodsContent.toString()
                + "\n</FH>\n"
                + "********************************\n";
        return content;
    }

}